package com.wolfeyes.framework.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
//import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.wolfeyes.framework.web.filter.RequestIdFilter;
import com.wolfeyes.framework.web.filter.WebContextFilter;
import com.wolfeyes.framework.web.serializer.CustomizeJackson2ObjectMapperBuilderCustomizer;

/**
 * @see Talk is cheap, Show me the code. -- Linus Torvalds
 * @title WebMVC通用资源的配置器组件Cros、requestfilter等
 * @author yanyljava
 * @create 2022-01-12 20:49
 */
@Configuration
public class WebMvcResourceConfig implements WebMvcConfigurer {
	
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("http://localhost:8080")
				.allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
				.allowCredentials(true)
				.maxAge(3600)
				.allowedHeaders("*");
	}
	
	/*
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
	// JDK8新特性之接口默认方法与静态方法 -> 空实现
	}
	*/

	/**
	 * @title 跨域资源的请求配置-开启跨域
	 * @return CorsFilter 跨域配置的过滤器
	 */
	@Bean
	public CorsFilter corsFilter() {
		
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		// 是否允许证书（cookies）
		config.setAllowCredentials(true);
		// 设置访问源地址
		// 设置允许跨域请求的域名
		//config.addAllowedOrigin("*");
		//config.addAllowedOrigin("http://localhost:80");
		//When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. 
		//To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
		//config.addAllowedOriginPattern("*");
		// 设置允许跨域请求的域名
		config.addAllowedOriginPattern("*");
		//config.setAllowCredentials(true);
		// 设置访问源请求头
		config.addAllowedHeader("*");
		// 设置访问源请求方法-设置允许的方法
		config.addAllowedMethod("*");
		// 前后端分离的项目Restful风格下接口标准：增C删D改U查R-CRUD => POST,DELETE,PUT,GET 以及跨域请求的前置OPTIONS请求
		//config.addAllowedMethod("GET,HEAD,POST,OPTIONS,PUT,DELETE");
		// 设置跨域允许时间
		// 超时设置24*60*60，单位：秒
		config.setMaxAge(86400L);
		// 对接口配置跨域设置
		// 设置允许跨域的路由
		source.registerCorsConfiguration("/**", config);
		return new CorsFilter(source);
	}
	
	/**
	 * @title web context 容器注入
	 */
	@Bean
	@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
	public FilterRegistrationBean<WebContextFilter> webContextFilter(){
		FilterRegistrationBean<WebContextFilter> registrationBean = new FilterRegistrationBean<>();
		registrationBean.setFilter(new WebContextFilter());
		registrationBean.setName("webContextFilter");
		registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
		return registrationBean;
	}

	/**
	 * @title request id 容器注入
	 */
	@Bean
	@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
	public FilterRegistrationBean<RequestIdFilter> requestIdFilter(){
		FilterRegistrationBean<RequestIdFilter> registrationBean = new FilterRegistrationBean<>();
		registrationBean.setFilter(new RequestIdFilter());
		registrationBean.setName("requestIdFilter");
		registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
		return registrationBean;
	}

	/**
	 * @title 自定义的json转对象的映射器构建类 容器注入
	 * @return Jackson2ObjectMapperBuilderCustomizer 自定义的json转对象的映射器
	 */
	//@Bean
	public Jackson2ObjectMapperBuilderCustomizer getJackson2ObjectMapperBuilderCustomizer() {
		
		// 解决前端Long形式丢失的问题
		// 自定义的json转对象的映射器构建类
		return new CustomizeJackson2ObjectMapperBuilderCustomizer();
	}
	
}


